<template>
  <section>
    <el-row style="display: flex;align-items: center;">
      <el-col :span="6">
        <el-input size="mini" placeholder="好像用不到？"  clearable v-model="searchKey" @keydown.native.prevent.enter="searchBtn" ></el-input>
      </el-col>
      <el-col :span="4">
        <i class="el-icon-search" @click="searchBtn" style="margin-left:10px" ></i>
      </el-col>
      <el-col :span="12" >
        <el-button size="mini" @click="addPhysical" style="float: right" type="primary">新建体质类型</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-table :data="physicalList" stripe v-loading="loading">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-width="80px" class="demo-table-expand">
              <el-form-item label="描述：">
                <TextEditor v-if="props.row.edit" v-model="props.row.description" :editorToolbar="customToolbar"></TextEditor>
                <!--<el-input v-show="props.row.edit" size="small" v-model="props.row.description"></el-input>-->
                <span v-if="!props.row.edit" v-html="props.row.description"></span>
              </el-form-item>
              <el-form-item label="原则：">
                <el-row>标题：
                  <el-input v-if="props.row.edit" size="small" v-model="props.row.metadata.title"></el-input>
                  <span  v-if="!props.row.edit">{{props.row.metadata.title?props.row.metadata.title:'没'}}</span>
                </el-row>
                <el-row>内容：
                  <el-input v-if="props.row.edit" size="small" v-model="props.row.metadata.principle"></el-input>
                  <span  v-if="!props.row.edit">{{props.row.metadata.principle?props.row.metadata.principle:'没'}}</span>
                </el-row>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="name" label="名称" width="100px">
          <template slot-scope="scope">
            <el-input v-if="scope.row.edit" size="small" v-model="scope.row.name"></el-input>
            <span  v-if="!scope.row.edit">{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型" >
          <template slot-scope="scope">
            <el-select v-model="scope.row.type" size="mini" v-if="scope.row.edit">
              <el-option :value="type.val" v-for="type in typeList" :key="type.val" :label="type.label">{{type.label}}</el-option>
            </el-select>
            <span v-if="!scope.row.edit">{{typeList.find(t=>t.val === scope.row.type).label}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="symptom" label="症状" >
          <template slot-scope="scope">
            <el-input v-if="scope.row.edit" size="small" v-model="scope.row.symptom"></el-input>
            <span  v-if="!scope.row.edit">{{scope.row.symptom}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button :type="scope.row.edit?'success':'primary'" @click='confirm(scope.row)' size="small" icon="edit">{{scope.row.edit?'完成':'编辑'}}</el-button>
            <el-button @click='cancelBtn(scope.row)' size="small" v-if="scope.row.edit">取消</el-button>
            <el-button @click='del(scope)' size="small" v-if="!scope.row.edit">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </section>
</template>

<script>
  import TextEditor from '@/components/TextEditor'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'
  export default {
    name: "physical",
    components:{TextEditor},
    data() {
      return {
        physicalList:null,
        loading:false,
        searchKey:null,
        customToolbar: [['bold','italic','underline'],[{'list':'ordered'},{'list':'bullet'}],['image','code-block']],
        typeList:[
          {label:'儿童',val:1},
          {label:'成人',val:2},
        ]
      }
    },
    created(){
      //加载体质列表
      this.getPhysicalList()
    },
    computed:{
    },
    watch: {
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      searchBtn() {
        this.getPhysicalList('', this.searchKey)
      },
      getPhysicalList(){
        this.fetch({name:"getPhysicalList"})
          .then(res=>{
            console.log(res)
            this.physicalList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      addPhysical() {
        //新建一个空的addAcupoint，表格页面+1栏
        this.fetch({name: "createPhysical", data: {name: '', symptom: '',description:'',type:1},method: 'post'})
          .then(res => {
            console.log(res)
            res.edit = true
            this.physicalList.push(res)
          })
          .catch(err => {
            console.log(err)
            errHandler.handle(this, err)
          })
      },
      confirm(row) {
        console.log(row)
        if (row.edit === undefined) {
          Vue.set(row, 'edit', true)//后续添加属性，需要vue联动
          return
        }
        if (row.edit) {
          this.loading = true
          this.updatePhysical(row)
        } else {
          row.edit = true
        }
      },
      cancelBtn(row) {//取消按钮
        if (row.edit === undefined) {
          Vue.set(row, 'edit', false)//后续添加属性，需要vue联动
        } else {
          row.edit = false
        }
      },
      del(scope) {//删除按钮
        this.loading = true
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name: "deletePhysical", data: {id: scope.row.id}})
              .then(res => {
                this.loading = false
                this.physicalList.splice(scope.$index, 1)
              })
              .catch(err => {
                console.log(err)
                errHandler.handle(this, err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      },
      updatePhysical(row) {
        this.fetch({name: "updatePhysical", data: {...row},method: 'post'})
          .then(res => {
            this.loading = false
            console.log(res)
            row.edit = false
          })
          .catch(err => {
            console.log(err)
            errHandler.handle(this, err)
          })
      }
    }
  }
</script>

<style scoped>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
